package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.CategoryVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.CategoryService;
import com.itheima.travel.utils.ExceptionsUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CategoryController.java
 * @Description 分类controller
 */
@RestController
@Log4j2
@CrossOrigin
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /***
     * @description 查询所有分类列表
     * @return: java.util.List<com.itheima.travel.req.CategoryVo>
     */
    @GetMapping("category")
    public ResponseWrap<List<CategoryVo>> findAllCategory() throws ProjectException {
        try {
            List<CategoryVo> allCategory = categoryService.findAllCategory();
            return ResponseWrap.<List<CategoryVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(allCategory)
                    .build();
        }catch (Exception e){
            log.error("查询分类：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ALL_CATEGORY_FAIL.getCode(),
                    StatusEnum.FIND_ALL_CATEGORY_FAIL.getMsg());
        }

    }
}
